//
//  StrategyButtonViewController.m
//  ITrip
//
//  Created by 1564335 on 15-6-1.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "StrategyButtonViewController.h"
#import "JourneyButtonTableViewCell.h"
#import "JourneyButtonView.h"
@interface StrategyButtonViewController ()<UIScrollViewDelegate>

{
    NSInteger tagNumber;
}
@property(nonatomic, retain) UIScrollView *strategyButtonScrollView;


@property(nonatomic, assign) NSInteger tableSection;

@property(nonatomic, assign) NSInteger tableRow;

@end

@implementation StrategyButtonViewController

- (void)dealloc
{
    [_strategyButtonScrollView release];
    [_arrayData release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIImage *icon = [UIImage imageNamed:@"iconfont_return"];
    icon = [icon imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:icon style:UIBarButtonItemStyleDone target:self action:@selector(getBack)];
    
    self.strategyButtonScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:self.strategyButtonScrollView];
    [self.strategyButtonScrollView release];
    NSInteger scrollViewSize = 0;
    self.strategyButtonScrollView.delegate = self
    ;
    for (StrategyForeignStrategyParser *foreignParser in self.arrayData) {
        scrollViewSize += foreignParser.pages.count;
    }
    
    self.strategyButtonScrollView.contentSize = CGSizeMake(self.view.frame.size.width *  scrollViewSize, self.view.frame.size.height);
    self.strategyButtonScrollView.pagingEnabled = YES;
    NSInteger num = 0;
    for (int i = 0; i < self.arrayData.count; i++) {
        StrategyForeignStrategyParser *foreign = [self.arrayData objectAtIndex:i];
        for (int j = 0; j < foreign.pages.count; j++) {
            StrategyPagesParser *pages = [foreign.pages objectAtIndex:j];
            JourneyButtonView *view = [[JourneyButtonView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * num++, 0,self.view.frame.size.width, self.view.frame.size.height) :pages];
            [self.strategyButtonScrollView addSubview:view];
            [view release];
        }
    }
    
    NSInteger number = 0;
    for (int i = 0; i < self.section + 1; i++) {
        StrategyForeignStrategyParser *foreign = [self.arrayData objectAtIndex:i];
        if (i != self.section) {
            for (int j = 0; j < foreign.pages.count; j++) {
                number++;
            }
        } else {
            for (int j = 0; j <= self.row; j++) {
                number++;
            }
        }
        
    }
    
    self.strategyButtonScrollView.contentOffset = CGPointMake((number - 1) * self.view.frame.size.width, 0);
    int i = 0, j = 0;
    number--;
    for (i = 0; i < self.arrayData.count; i++) {
        StrategyForeignStrategyParser *foreign = [self.arrayData objectAtIndex:i];
        for (j = 0; j < foreign.pages.count; j++) {
            number--;
            if (number + 1 == 0) {
                break;
            }
        }
        if (number + 1 == 0) {
            break;
        }
    }
    
    StrategyForeignStrategyParser *foreign = [self.arrayData objectAtIndex:i];
    StrategyPagesParser *pages = [foreign.pages objectAtIndex:j];
    self.navigationItem.title = pages.strategy_title;
    
    
}

- (void)getBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger number = scrollView.contentOffset.x / self.view.frame.size.width;
    int i = 0, j = 0;
    for (i = 0; i < self.arrayData.count; i++) {
        StrategyForeignStrategyParser *foreign = [self.arrayData objectAtIndex:i];
        for (j = 0; j < foreign.pages.count; j++) {
            number--;
            if (number + 1 == 0) {
                break;
            }
        }
        if (number + 1 == 0) {
            break;
        }
    }
    StrategyForeignStrategyParser *foreign = [self.arrayData objectAtIndex:i];
    StrategyPagesParser *pages = [foreign.pages objectAtIndex:j];
        self.navigationItem.title = pages.strategy_title;

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
